﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Text;
using System.Text.RegularExpressions;

/// <summary>
/// *========================
///  C# VB函数助手类
///  V1.1.100429 (CMS7)
///  by eXplorer
/// *========================
/// </summary>
namespace CMS.Helper
{
    public sealed class CommonFunctions
    {
        public CommonFunctions()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 从左截取字符串
        /// </summary>
        /// <param name="sSource">待处理字符串</param>
        /// <param name="iLength">结束位置</param>
        /// <returns>截获的子串</returns>
        public static string Left(string sSource, int iLength)
        {
            return sSource.Substring(0, iLength > sSource.Length ? sSource.Length : iLength);
        }
        /// <summary>
        /// 从右截取字符串
        /// </summary>
        /// <param name="sSource">待处理字符串</param>
        /// <param name="iLength">结束位置</param>
        /// <returns>截获的子串</returns>
        public static string Right(string sSource, int iLength)
        {
            return sSource.Substring(iLength > sSource.Length ? 0 : sSource.Length - iLength);
        }
        /// <summary>
        /// 从中间截取字符串
        /// </summary>
        /// <param name="sSource">待处理字符串</param>
        /// <param name="iStart">开始位置</param>
        /// <param name="iLength">结束位置</param>
        /// <returns></returns>
        public static string Mid(string sSource, int iStart, int iLength)
        {
            int iStartPoint = iStart > sSource.Length ? sSource.Length : iStart - 1;
            return sSource.Substring(iStartPoint, iStartPoint + iLength > sSource.Length ? sSource.Length - iStartPoint : iLength);
        }

        /// <summary>
        /// 判断是否为数字
        /// </summary>
        /// <param name="str">待判断的字符串</param>
        /// <returns>是否为数字</returns>
        public static bool IsNumeric(string str)
        {
            //是否整数
            Regex myRegex1 = new Regex("^-?\\d+$");
            bool isInt = myRegex1.IsMatch(str.Trim());
            //是否浮点数
            Regex myRegex2 = new Regex("^(-?\\d+)(\\.\\d+)?$");
            bool isFloat = myRegex2.IsMatch(str.Trim());
            //整数或浮点数 -> 是数值
            if(isInt || isFloat)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 是否为英文
        /// </summary>
        /// <param name="str"></param>
        /// <returns>是否为英文</returns>
        public static bool IsEnglish(string str)
        {
            Regex myRegex = new Regex("^[A-Za-z]+$");
            return myRegex.IsMatch(str.Trim());
        }

        
        public enum DateInterval
        {
            Second, Minute, Hour, Day, Week, Month, Quarter, Year
        }
        /// <summary>
        /// 计算日期间的跨度
        /// </summary>
        /// <param name="Interval">跨度单位</param>
        /// <param name="StartDate">起始日期</param>
        /// <param name="EndDate">终止日期</param>
        /// <returns>跨度</returns>
        public static long DateDiff(DateInterval Interval, System.DateTime StartDate, System.DateTime EndDate)
        {
            long lngDateDiffValue = 0;
            System.TimeSpan TS = new System.TimeSpan(EndDate.Ticks - StartDate.Ticks);
            switch (Interval)
            {
                case DateInterval.Second:
                    lngDateDiffValue = (long)TS.TotalSeconds;
                    break;
                case DateInterval.Minute:
                    lngDateDiffValue = (long)TS.TotalMinutes;
                    break;
                case DateInterval.Hour:
                    lngDateDiffValue = (long)TS.TotalHours;
                    break;
                case DateInterval.Day:
                    lngDateDiffValue = (long)TS.Days;
                    break;
                case DateInterval.Week:
                    lngDateDiffValue = (long)(TS.Days / 7);
                    break;
                case DateInterval.Month:
                    lngDateDiffValue = (long)(TS.Days / 30);
                    break;
                case DateInterval.Quarter:
                    lngDateDiffValue = (long)((TS.Days / 30) / 3);
                    break;
                case DateInterval.Year:
                    lngDateDiffValue = (long)(TS.Days / 365);
                    break;
            }
            return (lngDateDiffValue);
        }
    }
}